package adapterdata;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import datas.DrawerLayoutData;
import timor.timor.R;

/**
 * Created by aaa on 15-4-29.
 */
public class DrawerLayoutAdapter extends BaseAdapter{

    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<DrawerLayoutData> datas;

    public DrawerLayoutAdapter(Context context, ArrayList<DrawerLayoutData> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=inflater.inflate(R.layout.drawerlayout_list_item,null);
            holder=new ViewHolder();
            holder.icon1= (ImageView) convertView.findViewById(R.id.drawerlayout_icon1);
            holder.text1= (TextView) convertView.findViewById(R.id.drawerlayout_text1);
            holder.icon2= (ImageView) convertView.findViewById(R.id.drawerlayout_icon2);
            holder.text2= (TextView) convertView.findViewById(R.id.drawerlayout_text2);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        DrawerLayoutData data = (DrawerLayoutData) getItem(position);
        holder.icon1.setImageResource(data.getIcon1());
        holder.text1.setText(data.getTitle());
        if(position!=3){
            holder.icon2.setVisibility(View.GONE);
            if(data.getMessage()!=null){
                if(position==1){
                    holder.text2.setTextColor(Color.RED);
                }
                holder.text2.setVisibility(View.VISIBLE);
                holder.text2.setText(data.getMessage());

            }else {
                holder.text2.setVisibility(View.GONE);
            }
        }else {
            holder.icon2.setVisibility(View.VISIBLE);
            holder.icon2.setImageResource(data.getIcon2());
            holder.text2.setText(data.getMessage());
        }


        return convertView;
    }

    class ViewHolder{
        ImageView icon1;
        TextView text1;
        ImageView icon2;
        TextView text2;
    }

}
